
local excel = load_excel('商店设置.xlsx','迷宫挑战')

local mt = ac.skill['迷宫机制']
mt.skill_type = "passive"
mt.model1 = [[Abilities\Weapons\ZigguratFrostMissile\ZigguratFrostMissile.mdl]]

function mt:on_add()
    local hero = self.owner
    if ac.init_game==nil then
        return
    end
    hero:add_buff '预警机制'{}

    self:gc(function()
        local player = hero.owner_player
        player.mg_point = hero:get_point()
        local unit = player:create_unit('金矿',hero:get_point())
        unit.ui_name = '迷宫'
        unit:set_model([[buildings\undead\Crypt\Crypt.mdl]])
        player:set_data('引导-迷宫',true)
        player:notify('引导-显示内容',player)
        
        -- local time = 0
        -- local timer = unit:loop(30,function(t)
        --     time = time + 0.03
        --     if time>=120 then
        --         time = 0
        --         player:notify('玩家-发起迷宫挑战',player)
        --     end
        -- end)
        -- ac.game:event '游戏-关闭挑战'(function()
        --     timer:remove()
        -- end)
        --player:notify('玩家-发起迷宫挑战',player)
        player:notify('玩家-显示商店',player,'迷宫商店')
    end)
end

for a,da in ipairs(excel) do
    local name = da.name
    ac.skill(name){
        index = a,
        art = da.art,
        tip = da.tip,
        cool = 25,
        on_add = function(self)
            local unit = self.owner
            local player = unit.owner
            if self.index~=1 then
                self:set_enable(false)
            end
            self:gc(player:event '玩家-击败迷宫精英'(function(_,_,index)
                self.open = nil
                if index==self.index-1 then
                    self:set_enable(true)
                end
                if index==self.index then
                    self:remove()
                end
            end))
            ac.game:event '游戏-关闭挑战'(function()
                unit:remove()
            end)
        end,
        on_sell = function(self,hero,player)
            show_skill_tip(player,self,'刷新')
            player:notify('玩家-发起迷宫挑战',player,20,self.index)
        end,
    }
end


ac.skill('迷宫挑战'){
    count = 0,
    art = [[图标\迷宫挑战.blp]],
    tip = [[
[挑战奖励]
普通层随机属性/概率掉落装备
每3层获得大量金币/杀敌奖励，有概率掉落隐藏的随机奖励]],
    cool = 25,
    on_add = function(self)
        local unit = self.owner
        local player = unit.owner
        -- self:remove()
        -- for _,da in ipairs(excel) do
        --     unit:add_skill(da.name,'英雄')
        -- end
        self:gc(player:event '玩家-击败迷宫精英'(function(_,_,index)
            self.count = self.count + 1
            self:set_cd(0)
            if self.count>=9 then
                unit:remove()
            end
        end))
        ac.game:event '游戏-关闭挑战'(function()
            unit:remove()
        end)
    end,
    -- get_food = function(self)
    --     local unit = self.owner
    --     local player = unit.owner
    --     return 1000 + 500*player:get_data('迷宫挑战次数',0)
    -- end,
    on_sell = function(self,hero,player)
        show_skill_tip(player,self,'刷新')
        player:notify('玩家-发起迷宫挑战',player,20,self.count + 1)
    end
}


local weight1 = {}
local weight2 = {}
for id,v in pairs(load_excel('特殊奖池.xlsx','迷宫装备')) do
    weight1[#weight1 + 1] = {id,v['普通权重']}
    weight2[#weight2 + 1] = {id,v['精英权重']}
end

local weight_items = {}
for name,da in pairs(load_excel('特殊奖池.xlsx','迷宫奖励')) do
    weight_items[#weight_items + 1] = {name,da['权重']}
end

local levels = load_excel('数据投放.xlsx','挑战等级')
ac.game:event '玩家-发起迷宫挑战'(function(_,player,time,index)
    local point = player.mg_point
    local hero = player.hero
    local level = levels[index]['迷宫挑战']
    if index%3~=0 then
        local max = 10
        local num = max
        ac.timer(500,max,function()
            local unit = player:create_enemy('探索怪',point)
            if time then
                unit:set_enemy_level(level)
                unit:add_buff '挑战限时'{time = time}
            end
            unit:event '单位-死亡'(function()
                max = max - 1
                if max<=0 then
                    player:notify('玩家-击败迷宫精英',player,index)
                    for a=1,num do
                        hero:notify('英雄-探索奖励',hero)
                    end
                end
            end)
        end)
        return
    else
        local max = 5
        ac.timer(500,max,function()
            local unit = player:create_enemy('迷宫怪',point)
            unit:add_buff '自动仇恨'{}
            if time then
                unit:set_enemy_level(level)
                unit:add_buff '挑战限时'{time = time}
            end
        end)
        ac.wait(3000,function()
            local unit = player:create_enemy('迷宫精英',point)
            unit.index = index
            unit:add_buff '自动仇恨'{}
            if time then
                unit:add_buff '挑战限时'{time = time}
                unit:set_enemy_level(level)
            end
            unit:event '单位-死亡'(function()
                player:add_icon('杀敌',1000)
                player:add_icon('金币',10000)
                player:notify('玩家-击败迷宫精英',player,index)
                for a=1,2 do
                    local name = table.get_by_weight(weight_items)
                    if name~='觉醒石' then
                        local item = collate_item(hero,name)
                        player:send_msg(('获得：%s'):format(item:get_title()))
                    else
                        local item = hero:create_jx_item()
                        collate_item(hero,item)
                        player:send_msg(('获得：%s'):format(item:get_title()))
                    end
                end
            end)
        end)
    end

end)
